from typing import List


class Solution:
    def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
        N=max(end for _,_,end in trips)+2
        diff=[0]*N
        for i,start,end in trips:
            diff[start]+=i
            # 区间的右范围 不是 end+1 是因为 在终点站end时，人已经下车了，当前end点，人数应该为0
            diff[end]-=i
        pre=[0]*N
        pre[0]=diff[0]
        for i in range(1,N):
            pre[i]=pre[i-1]+diff[i]
        if max(pre)>capacity:
            return False
        return True


